package com.doubao.notepad_blog_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.doubao.notepad_blog_backend.common.api.ApiResult;
import com.doubao.notepad_blog_backend.domain.BmsComment;
import com.doubao.notepad_blog_backend.domain.UmsUser;
import com.doubao.notepad_blog_backend.dto.CommentDTO;
import com.doubao.notepad_blog_backend.service.BmsCommentService;
import com.doubao.notepad_blog_backend.service.UmsUserService;
import com.doubao.notepad_blog_backend.vo.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.doubao.notepad_blog_backend.jwt.JwtUtil.USER_NAME;

@RestController
@RequestMapping("/comment")
@Api(value = "评论管理",tags = {"评论管理"})
public class BmsCommentController {
    @Resource
    private BmsCommentService commentService;
    @Resource
    private UmsUserService userService;

    @ApiOperation( "文章的所有评论" )
    @GetMapping("/get_comments")
    public ApiResult<List<CommentVO>> getCommentsByPostID(@RequestParam(value = "postid", defaultValue = "1") String postid) {
        List<CommentVO> lstBmsComment = commentService.getCommentsByPostID(postid);
        return ApiResult.success(lstBmsComment);
    }

    @ApiOperation( "添加评论" )
    @PostMapping("/add_comment")
    public ApiResult<BmsComment> addComment(@RequestHeader(value = USER_NAME) String userName,
                                            @RequestBody CommentDTO dto) {
        UmsUser umsUser = userService.getOne(new LambdaQueryWrapper<UmsUser>().eq(UmsUser::getUsername,userName));

        Assert.notNull(umsUser,"用户不存在");
        BmsComment comment = commentService.addComment(dto, umsUser);
        return ApiResult.success(comment);
    }



}
